//
//  NSDateFormatter+Extension.m
//  JQLive
//
//  Created by Bruce on 2020/1/19.
//  Copyright © 2020 mlqk. All rights reserved.
//

#import "NSDateFormatter+Extension.h"


@implementation NSDateFormatter (Extension)
/**
 NsData 转换为时间戳
 */
+ (long long)getDateTimeTOMilliSeconds:(NSDate *)datetime
{
    NSTimeInterval interval = [datetime timeIntervalSince1970];
    long long totalMilliseconds = interval*1000 ;
    return totalMilliseconds;
}

//时间戳转nddata
+ (NSDate *)UTCDateFromTimeStamap:(NSString *)timeStamap{

    NSTimeInterval timeInterval = [timeStamap doubleValue]/1000; //精确到毫秒
    NSDate* date = [NSDate dateWithTimeIntervalSince1970: timeInterval];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate: date];
    NSDate *localeDate = [date dateByAddingTimeInterval: interval];
    
    return localeDate;
}


/**
 时间戳转化为时间
 
 @param time 时间戳
 @param str 格式化需求
 @return  目标时间
 */
+ (NSString *)getTimeWithInterval:(NSString*)time withFormatStr:(NSString*)str{
    NSDate *timeDate    = [[NSDate alloc]initWithTimeIntervalSince1970:[time longLongValue]/1000.0];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:str];
    NSString *dateString = [dateFormatter stringFromDate:timeDate];
    return dateString;
}

+ (NSString *)getTimeFromTimestamp:(NSString *)timeStamp timeFormat:(NSString *)format{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setTimeZone:timeZone];
    [formatter setDateFormat:format];
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[timeStamp integerValue]];
    NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
    return confromTimespStr;
}

/**
 时间转换为时间戳
 
 @param timeStr 日期时间字符串
 @param str 时间字符串规则
 @return 对应的时间截
 */
+ (NSString *)getTimeIntervalWithTimeStr:(NSString*)timeStr withFormatStr:(NSString*)str {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:str];
    NSDate *date = [dateFormatter dateFromString:timeStr];
    NSTimeInterval timeInterval = [date timeIntervalSince1970];
    long long timeIntervalF = timeInterval  * 1000;
    return [NSString stringWithFormat:@"%lld", timeIntervalF];
}

#pragma mark - 判断

/**
 判断两个时间前后
 
 @param oneDay 第一个时间
 @param anotherDay 第二个时间
 @return 前后结果  1 说明第一个时间在将来  -1 说明第一个时间在过去 0 两个时间一样
 */
+ (int)compareOneDay:(NSDate *)oneDay withAnotherDay:(NSDate *)anotherDay
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSString *oneDayStr = [dateFormatter stringFromDate:oneDay];
    NSString *anotherDayStr = [dateFormatter stringFromDate:anotherDay];
    NSDate *dateA = [dateFormatter dateFromString:oneDayStr];
    NSDate *dateB = [dateFormatter dateFromString:anotherDayStr];
    NSComparisonResult result = [dateA compare:dateB];
    NSLog(@"date1 : %@, date2 : %@", oneDay, anotherDay);
    if (result == NSOrderedDescending) {//将来
        return 1;
    }
    else if (result == NSOrderedAscending){//过去
        return -1;
    }
    return 0;
}

#pragma mark - 实用

/**
 获取年龄
 */
+ (NSInteger)ageWithDateOfBirth:(NSDate *)date;{
    NSDateComponents *components1 = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:date];    NSInteger brithDateYear  = [components1 year];    NSInteger brithDateDay   = [components1 day];    NSInteger brithDateMonth = [components1 month];
    NSDateComponents *components2 = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]];    NSInteger currentDateYear  = [components2 year];    NSInteger currentDateDay   = [components2 day];    NSInteger currentDateMonth = [components2 month];
    NSInteger iAge = currentDateYear - brithDateYear - 1;
    if ((currentDateMonth > brithDateMonth) || (currentDateMonth == brithDateMonth && currentDateDay >= brithDateDay)) {
        iAge++;
    }
    return iAge;
}

/**
 * 获取当前时间戳 NSString类型
 */
+ (NSString *)getTimeNow
{
    NSString* date;
    NSDateFormatter * formatter = [[NSDateFormatter alloc ]init];
    [formatter setDateFormat:@"YYYY-MM-dd hh:mm:ss:SSS"];
    date = [formatter stringFromDate:[NSDate date]];
    NSString* timeNow = [[NSString alloc] initWithFormat:@"%@", date];
    NSLog(@"当前时间：%@", timeNow);
    return timeNow;
}

/**
 * 获取当前时间 NSString类型 指定格式
 */
+ (NSString *)getCurrentTimeNow:(NSString*)format
{
    NSString * date;
    NSDateFormatter * formatter = [[NSDateFormatter alloc ]init];
    [formatter setDateFormat:format];
    date = [formatter stringFromDate:[NSDate date]];
    NSString* timeNow = [[NSString alloc] initWithFormat:@"%@", date];
    NSLog(@"当前时间：%@", timeNow);
    return timeNow;
}



#pragma mark 微博时间
/**
 *  类别                             格式
 *  (1)今年
 *         ①今天
 *              a.一分钟内            刚刚
 *              b.1分钟～59分钟之间    xx分钟前
 *              c.大于等于60分钟       xx小时前
 *         ②昨天                     昨天 xx:xx （时：分）
 *         ③今年的其他日子             xx-xx
 *
 *  (2)非今年                         xxxx-xx-xx
 */
+ (NSString *)compareCurrentTime:(NSString *)timeStr {
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    // 设置格式
    fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    // 发布日期对象
    NSDate *createDate = [[NSDate alloc]initWithTimeIntervalSince1970:[timeStr longLongValue]/1000.0];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    // 获取两个日期差值
    NSDateComponents *cmp = [calendar components:NSCalendarUnitHour | NSCalendarUnitMinute fromDate:createDate toDate:[NSDate date] options:NSCalendarWrapComponents];
    if ([createDate isThisYear]) {
        if ([createDate isToday]) {
            // 获取日期差值
            if (cmp.hour >= 1) {
                timeStr = [NSString stringWithFormat:@"%ld小时前",(long)cmp.hour];
            }else if (cmp.minute >= 2) {
                timeStr = [NSString stringWithFormat:@"%ld分钟前",(long)cmp.minute];
            }else { // 刚刚
                timeStr = @"刚刚";
            }
        } else if ([createDate isYesterday]) { // 昨天
            // 昨天 21:10
            fmt.dateFormat = @"昨天 HH:mm";
            timeStr = [fmt stringFromDate:createDate];
            
        } else { // 今年的其他日子
            fmt.dateFormat = @"MM月dd日";
            timeStr = [fmt stringFromDate:createDate];
        }
    } else { // 非今年
        fmt.dateFormat = @"yyyy年MM月dd日";
        timeStr = [fmt stringFromDate:createDate];
    }
    return timeStr;
}

@end
